package com.ruoyi.tzai.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.tzai.mapper.UserPlayHistoryMapper;
import com.ruoyi.tzai.domain.UserPlayHistory;
import com.ruoyi.tzai.service.IUserPlayHistoryService;
import com.ruoyi.common.utils.DateUtils;

/**
 * 用户播放历史Service业务层处理
 * 
 * @author tzai
 * @date 2025-10-27
 */
@Service
public class UserPlayHistoryServiceImpl implements IUserPlayHistoryService 
{
    @Autowired
    private UserPlayHistoryMapper userPlayHistoryMapper;

    /**
     * 查询用户播放历史
     * 
     * @param historyId 用户播放历史主键
     * @return 用户播放历史
     */
    @Override
    public UserPlayHistory selectUserPlayHistoryByHistoryId(Long historyId)
    {
        return userPlayHistoryMapper.selectUserPlayHistoryByHistoryId(historyId);
    }

    /**
     * 根据用户ID和文章ID查询用户播放历史
     * 
     * @param userId 用户ID
     * @param articleId 文章ID
     * @return 用户播放历史
     */
    @Override
    public UserPlayHistory selectUserPlayHistoryByUserIdAndArticleId(Long userId, Long articleId)
    {
        return userPlayHistoryMapper.selectUserPlayHistoryByUserIdAndArticleId(userId, articleId);
    }

    /**
     * 查询用户播放历史列表
     * 
     * @param userPlayHistory 用户播放历史
     * @return 用户播放历史
     */
    @Override
    public List<UserPlayHistory> selectUserPlayHistoryList(UserPlayHistory userPlayHistory)
    {
        return userPlayHistoryMapper.selectUserPlayHistoryList(userPlayHistory);
    }

    /**
     * 新增用户播放历史
     * 
     * @param userPlayHistory 用户播放历史
     * @return 结果
     */
    @Override
    @Transactional
    public int insertUserPlayHistory(UserPlayHistory userPlayHistory)
    {
        userPlayHistory.setCreateTime(DateUtils.getNowDate());
        userPlayHistory.setLastPlayTime(DateUtils.getNowDate());
        return userPlayHistoryMapper.insertUserPlayHistory(userPlayHistory);
    }

    /**
     * 修改用户播放历史
     * 
     * @param userPlayHistory 用户播放历史
     * @return 结果
     */
    @Override
    @Transactional
    public int updateUserPlayHistory(UserPlayHistory userPlayHistory)
    {
        userPlayHistory.setLastPlayTime(DateUtils.getNowDate());
        return userPlayHistoryMapper.updateUserPlayHistory(userPlayHistory);
    }

    /**
     * 批量删除用户播放历史
     * 
     * @param historyIds 需要删除的用户播放历史主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteUserPlayHistoryByHistoryIds(Long[] historyIds)
    {
        return userPlayHistoryMapper.deleteUserPlayHistoryByHistoryIds(historyIds);
    }

    /**
     * 删除用户播放历史信息
     * 
     * @param historyId 用户播放历史主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteUserPlayHistoryByHistoryId(Long historyId)
    {
        return userPlayHistoryMapper.deleteUserPlayHistoryByHistoryId(historyId);
    }

    /**
     * 更新播放进度
     * 
     * @param userId 用户ID
     * @param articleId 文章ID
     * @param language 播放语言
     * @param playProgress 播放进度（秒）
     * @param duration 总时长（秒）
     * @return 结果
     */
    @Override
    @Transactional
    public int updatePlayProgress(Long userId, Long articleId, String language, Integer playProgress, Integer duration)
    {
        // 查询是否已有播放记录
        UserPlayHistory existing = userPlayHistoryMapper.selectUserPlayHistoryByUserIdAndArticleId(userId, articleId);
        
        if (existing != null) {
            // 更新现有记录
            existing.setLanguage(language);
            existing.setPlayProgress(playProgress);
            existing.setDuration(duration);
            existing.setLastPlayTime(DateUtils.getNowDate());
            
            // 判断是否播放完成（进度达到95%以上视为完成）
            if (duration != null && duration > 0) {
                double progress = (double) playProgress / duration;
                existing.setIsFinished(progress >= 0.95 ? "1" : "0");
            }
            
            return userPlayHistoryMapper.updateUserPlayHistory(existing);
        } else {
            // 新增播放记录
            UserPlayHistory history = new UserPlayHistory();
            history.setUserId(userId);
            history.setArticleId(articleId);
            history.setLanguage(language);
            history.setPlayProgress(playProgress);
            history.setDuration(duration);
            history.setIsFinished("0");
            history.setCreateTime(DateUtils.getNowDate());
            history.setLastPlayTime(DateUtils.getNowDate());
            
            return userPlayHistoryMapper.insertUserPlayHistory(history);
        }
    }
}

